/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.jiujiudevice.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.jiujiudevice.entity.JiuUserAgree;
import com.boxin.ims.modules.jiujiudevice.service.JiuUserAgreeService;

/**
 * 用户协议Controller
 * @author sxh
 * @version 2018-11-14
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/jiujiudevice/jiuUserAgree")
public class JiuUserAgreeController extends BaseController {

	private static final Long GLOBAL_ID = 1L;//配置1条
	@Autowired
	private JiuUserAgreeService jiuUserAgreeService;
	
	@ModelAttribute
	public JiuUserAgree get() {
		JiuUserAgree jiuUserAgree = jiuUserAgreeService.get(1L);
			return jiuUserAgree;
	}
	
	@RequiresPermissions("jiujiudevice:jiuUserAgree:view")
	@RequestMapping(value = "form")
	public String form(JiuUserAgree jiuUserAgree, Model model) {
		
		model.addAttribute("jiuUserAgree", jiuUserAgree);
		return "modules/jiujiudevice/jiuUserAgreeForm";
	}

	@RequiresPermissions("jiujiudevice:jiuUserAgree:edit")
	@RequestMapping(value = "save")
	public String save(JiuUserAgree jiuUserAgree, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, jiuUserAgree)){
			return form(jiuUserAgree, model);
		}
		JiuUserAgree jiu = jiuUserAgreeService.get(GLOBAL_ID);
		if(jiu != null){
			jiu.setCreateTime(new Date());
			jiu.setContent(jiuUserAgree.getContent());
			jiuUserAgreeService.save(jiu);
		}else{
			jiuUserAgreeService.save(jiuUserAgree);
		}
		//设置配置唯一
		jiuUserAgree.setId(1L);
		jiuUserAgree.setContent(StringEscapeUtils.unescapeHtml(jiuUserAgree.getContent()));
		jiuUserAgreeService.save(jiuUserAgree);
		addMessage(redirectAttributes, "保存用户协议成功");
		return "redirect:"+Global.ADMIN_PATH+"/jiujiudevice/jiuUserAgree/form?repage";
	}
	
	@RequiresPermissions("jiujiudevice:jiuUserAgree:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		jiuUserAgreeService.delete(id);
		addMessage(redirectAttributes, "删除用户协议成功");
		return "redirect:"+Global.ADMIN_PATH+"/jiujiudevice/jiuUserAgree/?repage";
	}

}
